myApp.controller('goods', ['$scope', function($scope) {
	$scope.point = 'search';
}]);


myApp.controller('searchGoods', ['$scope', '$http', function($scope, $http) {
	$scope.allGoods = [];
	$scope.info = "";
	$scope.classId = "";
	$scope.minPrice = "";
	$scope.maxPrice = "";
	$scope.brandId = "";
	var allGoods_ = [];
	var allColor = $scope.top_Color;
	var allSize = $scope.top_Size;
	var allBrand = $scope.top_Brand;
	var allClass = $scope.top_Class;
	var allStyle = $scope.top_Style;

	$scope.searchGoodsByID = function() {
		if (!Number($scope.info)) {
			alert("请输入整数的ID号！");
			return;
		}

		var goodsData = {
			"goods.goodsId": $scope.info
		};

		var postCfg = {
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			transformRequest: function(data) {
				return $.param(data);
			}
		};

		$http.post('searchGoodsByID', goodsData, postCfg).then(function successCallback(response) {
			searchGoodsSuccess(response.data);
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});
	};

	$scope.searchGoodsByName = function() {
		if ($scope.info === "") {
			alert("请输入名称！");
			return;
		}

		var goodsData = {
			"goods.name": $scope.info
		};

		var postCfg = {
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			transformRequest: function(data) {
				return $.param(data);
			}
		};

		$http.post('searchGoodsByName', goodsData, postCfg).then(function successCallback(response) {
			searchGoodsSuccess(response.data);
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});
	};

	$scope.searchAllGoods = function() {
		$http.get('searchAllGoods').then(function successCallback(response) {
			searchGoodsSuccess(response.data);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});
	};

	$scope.searchGoodsByClassId = function() {
		if ($scope.classId === "" || $scope.classId === null) {
			alert("请选择类型！");
			return;
		}

		var goodsData = {
			"goods.classId": $scope.classId
		};

		var postCfg = {
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			transformRequest: function(data) {
				return $.param(data);
			}
		};

		$http.post('searchGoodsByClassId', goodsData, postCfg).then(function successCallback(response) {
			searchGoodsSuccess(response.data);
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});
	};

	$scope.searchGoodsByPrice = function() {
		if ($scope.minPrice === "" || $scope.minPrice === null) {
			alert("请填写最低价格！");
			return;
		} else if ($scope.maxPrice === "" || $scope.maxPrice === null) {
			alert("请填写最高价格！");
			return;
		}

		var goodsData = {
			"minPrice": $scope.minPrice,
			"maxPrice": $scope.maxPrice
		};

		var postCfg = {
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			transformRequest: function(data) {
				return $.param(data);
			}
		};

		$http.post('searchGoodsByPrice', goodsData, postCfg).then(function successCallback(response) {
			searchGoodsSuccess(response.data);
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});
	};

	$scope.searchGoodsByBrandId = function() {
		if ($scope.brandId === "" || $scope.brandId === null) {
			alert("请选择品牌！");
			return;
		}

		var goodsData = {
			"goods.brandId": $scope.brandId
		};

		var postCfg = {
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			transformRequest: function(data) {
				return $.param(data);
			}
		};

		$http.post('searchGoodsByBrandId', goodsData, postCfg).then(function successCallback(response) {
			searchGoodsSuccess(response.data);
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});
	};

	function searchGoodsSuccess(data) {
		if (data[0].Nothing === "Nothing") {
			alert("查询不到！");
			$scope.allGoods = [];
		} else {
			allGoods_ = transformGoods(data, "colorId", "name", allColor);
			allGoods_ = transformGoods(allGoods_, "sizeId", "name", allSize);
			allGoods_ = transformGoods(allGoods_, "brandId", "name", allBrand);
			allGoods_ = transformGoods(allGoods_, "classId", "name", allClass);
			allGoods_ = transformGoods(allGoods_, "styleId", "name", allStyle);
			$scope.allGoods = allGoods_;
		}
	}

	//将Goods的ID（color、size、class、style、brand）转换为相应的name
	//ID、name为Goods的属性，比如goods.colorID、goods.name;allObj为ID映射的对象组，比如allColor
	function transformGoods(allGoods, ID, name, allObjs) {
		for (var i = 0; i < allGoods.length; i++) {
			//获取ID的数组
			//ID为字符串的利用“，”分隔为数组，ID为int的直接以ID为数组
			if (typeof allGoods[i][ID] === "string") {
				var Id_ = allGoods[i][ID].split(",");
				if (Id_[0] === "") {
					allGoods[i][ID] = "";
					continue;
				}
			} else {
				var Id_ = [allGoods[i][ID]];
			}

			var name_ = "";
			for (var j = 0; j < Id_.length; j++) {
				var obj = findObjByID(Id_[j], allObjs, ID);
				//将所有的name组装成字符串
				if (j === (Id_.length - 1)) {
					name_ = name_ + obj[name];
				} else {
					name_ = name_ + obj[name] + ",";
				}
			}
			allGoods[i][ID] = name_;
		}
		return allGoods;
	}

	function findObjByID(Id, allObjs, ID) {
		for (var i = 0; i < allObjs.length; i++) {
			if (Id == allObjs[i][ID]) {
				return allObjs[i];
			}
		}
	}

	$scope.deleteGoodsById = function(goodsId) {
		var goodsData = {
			"goods.goodsId": goodsId
		};

		var postCfg = {
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			transformRequest: function(data) {
				return $.param(data);
			}
		};

		$http.post('deleteGoods', goodsData, postCfg).then(function successCallback(response) {
			alert("已删除成功！");
			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("删除操作失败!");
			console.log("fail：" + response);
		});
	};

}]);


myApp.controller('addGoods', ['$scope', '$http', function($scope, $http) {
	$scope.name = "";
	$scope.price = "";
	$scope.brandId = "";
	$scope.description = "无";
	$scope.remark = "无";

	$scope.colorId = "";
	$scope.selectedColor = [];
	$scope.sizeId = "";
	$scope.selectedSize = [];
	$scope.classId = "";
	$scope.selectedClass = [];
	$scope.styleId = "";
	$scope.selectedStyle = [];

	$scope.addGoods = function() {
		if ($scope.name === "") {
			alert("请填写服装名称！");
		} else if ($scope.price === "") {
			alert("请填写价格！");
		} else if ($scope.brandId === "" || $scope.brandId === null) {
			alert("请选择品牌！");
		} else if ($scope.description === "") {
			alert("请填写描述！");
		} else if ($scope.remark === "") {
			alert("请填写备注！");
		} else if ($scope.selectedColor.length === 0) {
			alert("请选择颜色！");
		} else if ($scope.selectedSize.length === 0) {
			alert("请选择尺寸！");
		} else if ($scope.selectedClass.length === 0) {
			alert("请选择类型！");
		} else {
			var colorId = "";
			for (var i = 0; i < $scope.selectedColor.length; i++) {
				if (i === ($scope.selectedColor.length - 1)) {
					colorId += $scope.selectedColor[i].colorId;
				} else {
					colorId += $scope.selectedColor[i].colorId + ",";
				}
			}

			var sizeId = "";
			for (var i = 0; i < $scope.selectedSize.length; i++) {
				if (i === ($scope.selectedSize.length - 1)) {
					sizeId += $scope.selectedSize[i].sizeId;
				} else {
					sizeId += $scope.selectedSize[i].sizeId + ",";
				}
			}

			var brandId = $scope.brandId;

			var classId = "";
			for (var i = 0; i < $scope.selectedClass.length; i++) {
				if (i === ($scope.selectedClass.length - 1)) {
					classId += $scope.selectedClass[i].classId;
				} else {
					classId += $scope.selectedClass[i].classId + ",";
				}
			}

			var styleId = "";
			for (var i = 0; i < $scope.selectedStyle.length; i++) {
				if (i === ($scope.selectedStyle.length - 1)) {
					styleId += $scope.selectedStyle[i].styleId;
				} else {
					styleId += $scope.selectedStyle[i].styleId + ",";
				}
			}

			var name = $scope.name;
			var price = $scope.price;
			var description = $scope.description;
			var remark = $scope.remark;

			var goodsData = {
				"goods.colorId": colorId,
				"goods.sizeId": sizeId,
				"goods.brandId": brandId,
				"goods.classId": classId,
				"goods.styleId": styleId,
				"goods.colorId": colorId,
				"goods.name": name,
				"goods.price": price,
				"goods.description": description,
				"goods.remark": remark
			};

			var postCfg = {
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				transformRequest: function(data) {
					return $.param(data);
				}
			};

			$http.post('addGoods', goodsData, postCfg).then(function successCallback(response) {
				alert("已添加成功！");
				$scope.name = "";
				$scope.price = "";
				$scope.brandId = "";
				$scope.description = "无";
				$scope.remark = "无";
				$scope.colorId = "";
				$scope.selectedColor = [];
				$scope.sizeId = "";
				$scope.selectedSize = [];
				$scope.classId = "";
				$scope.selectedClass = [];
				$scope.styleId = "";
				$scope.selectedStyle = [];

				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
	};

}]);

myApp.controller('setColor', ['$scope', function($scope) {
	var allColor = $scope.top_Color;
	$scope.addColor = function(colorId) {
		if (findColorByID2(colorId)) {
			$scope.selectedColor.push(findColorByID1(colorId));
		}
	};

	$scope.deleteColor = function(colorId) {
		for (var i = 0; i < $scope.selectedColor.length; i++) {
			if ($scope.selectedColor[i].colorId === colorId) {
				$scope.selectedColor.splice(i, 1);
				break;
			}
		}
	};

	function findColorByID1(ID) {
		for (var i = 0; i < allColor.length; i++) {
			if (ID === allColor[i].colorId) {
				return allColor[i];
			}
		}
	}

	function findColorByID2(ID) {
		for (var i = 0; i < $scope.selectedColor.length; i++) {
			if (ID === $scope.selectedColor[i].colorId) {
				return false;
			}
		}
		return true;
	}

}]);

myApp.controller('setSize', ['$scope', function($scope) {
	var allSize = $scope.top_Size;
	$scope.addSize = function(sizeId) {
		if (findSizeByID2(sizeId)) {
			$scope.selectedSize.push(findSizeByID1(sizeId));
		}
	};

	$scope.deleteSize = function(sizeId) {
		for (var i = 0; i < $scope.selectedSize.length; i++) {
			if ($scope.selectedSize[i].sizeId === sizeId) {
				$scope.selectedSize.splice(i, 1);
				break;
			}
		}
	};

	function findSizeByID1(ID) {
		for (var i = 0; i < allSize.length; i++) {
			if (ID === allSize[i].sizeId) {
				return allSize[i];
			}
		}
	}

	function findSizeByID2(ID) {
		for (var i = 0; i < $scope.selectedSize.length; i++) {
			if (ID === $scope.selectedSize[i].sizeId) {
				return false;
			}
		}
		return true;
	}

}]);

myApp.controller('setClass', ['$scope', function($scope) {
	var allClass = $scope.top_Class;
	$scope.addClass = function(classId) {
		if (findClassByID2(classId)) {
			$scope.selectedClass.push(findClassByID1(classId));
		}
	};

	$scope.deleteClass = function(classId) {
		for (var i = 0; i < $scope.selectedClass.length; i++) {
			if ($scope.selectedClass[i].classId === classId) {
				$scope.selectedClass.splice(i, 1);
				break;
			}
		}
	};

	function findClassByID1(ID) {
		for (var i = 0; i < allClass.length; i++) {
			if (ID === allClass[i].classId) {
				return allClass[i];
			}
		}
	}

	function findClassByID2(ID) {
		for (var i = 0; i < $scope.selectedClass.length; i++) {
			if (ID === $scope.selectedClass[i].classId) {
				return false;
			}
		}
		return true;
	}

}]);

myApp.controller('setStyle', ['$scope', function($scope) {
	var allStyle = $scope.top_Style;
	$scope.addStyle = function(styleId) {
		if (findStyleByID2(styleId)) {
			$scope.selectedStyle.push(findStyleByID1(styleId));
		}
	};

	$scope.deleteStyle = function(styleId) {
		for (var i = 0; i < $scope.selectedStyle.length; i++) {
			if ($scope.selectedStyle[i].styleId === styleId) {
				$scope.selectedStyle.splice(i, 1);
				break;
			}
		}
	};

	function findStyleByID1(ID) {
		for (var i = 0; i < allStyle.length; i++) {
			if (ID === allStyle[i].styleId) {
				return allStyle[i];
			}
		}
	}

	function findStyleByID2(ID) {
		for (var i = 0; i < $scope.selectedStyle.length; i++) {
			if (ID === $scope.selectedStyle[i].styleId) {
				return false;
			}
		}
		return true;
	}

}]);

myApp.controller('modifyGoods', ['$scope', '$http', function($scope, $http) {
	$scope.id = "";
	$scope.name = "";
	$scope.price = "";
	$scope.brandId = "";
	$scope.description = "无";
	$scope.remark = "无";

	$scope.colorId = "";
	$scope.selectedColor = [];
	$scope.sizeId = "";
	$scope.selectedSize = [];
	$scope.classId = "";
	$scope.selectedClass = [];
	$scope.styleId = "";
	$scope.selectedStyle = [];

	$scope.modifyGoods = function() {
		if ($scope.id === "" || $scope.id === null) {
			alert("请填写服装ID！");
		} else if ($scope.name === "") {
			alert("请填写服装名称！");
		} else if ($scope.price === "") {
			alert("请填写价格！");
		} else if ($scope.brandId === "" || $scope.brandId === null) {
			alert("请选择品牌！");
		} else if ($scope.description === "") {
			alert("请填写描述！");
		} else if ($scope.remark === "") {
			alert("请填写备注！");
		} else if ($scope.selectedColor.length === 0) {
			alert("请选择颜色！");
		} else if ($scope.selectedSize.length === 0) {
			alert("请选择尺寸！");
		} else if ($scope.selectedClass.length === 0) {
			alert("请选择类型！");
		} else {
			var goodsId = $scope.id;

			var colorId = "";
			for (var i = 0; i < $scope.selectedColor.length; i++) {
				if (i === ($scope.selectedColor.length - 1)) {
					colorId += $scope.selectedColor[i].colorId;
				} else {
					colorId += $scope.selectedColor[i].colorId + ",";
				}
			}

			var sizeId = "";
			for (var i = 0; i < $scope.selectedSize.length; i++) {
				if (i === ($scope.selectedSize.length - 1)) {
					sizeId += $scope.selectedSize[i].sizeId;
				} else {
					sizeId += $scope.selectedSize[i].sizeId + ",";
				}
			}

			var brandId = $scope.brandId;

			var classId = "";
			for (var i = 0; i < $scope.selectedClass.length; i++) {
				if (i === ($scope.selectedClass.length - 1)) {
					classId += $scope.selectedClass[i].classId;
				} else {
					classId += $scope.selectedClass[i].classId + ",";
				}
			}

			var styleId = "";
			for (var i = 0; i < $scope.selectedStyle.length; i++) {
				if (i === ($scope.selectedStyle.length - 1)) {
					styleId += $scope.selectedStyle[i].styleId;
				} else {
					styleId += $scope.selectedStyle[i].styleId + ",";
				}
			}

			var name = $scope.name;
			var price = $scope.price;
			var description = $scope.description;
			var remark = $scope.remark;

			var goodsData = {
				"goods.goodsId": goodsId,
				"goods.colorId": colorId,
				"goods.sizeId": sizeId,
				"goods.brandId": brandId,
				"goods.classId": classId,
				"goods.styleId": styleId,
				"goods.colorId": colorId,
				"goods.name": name,
				"goods.price": price,
				"goods.description": description,
				"goods.remark": remark
			};

			var postCfg = {
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				transformRequest: function(data) {
					return $.param(data);
				}
			};

			$http.post('modifyGoods', goodsData, postCfg).then(function successCallback(response) {
				alert("已修改成功！");
				$scope.id = "";
				$scope.name = "";
				$scope.price = "";
				$scope.brandId = "";
				$scope.description = "无";
				$scope.remark = "无";
				$scope.colorId = "";
				$scope.selectedColor = [];
				$scope.sizeId = "";
				$scope.selectedSize = [];
				$scope.classId = "";
				$scope.selectedClass = [];
				$scope.styleId = "";
				$scope.selectedStyle = [];

				console.log("ok：" + response);
			}, function errorCallback(response) {
				console.log("fail：" + response);
			});
		}
	};

	var allColor = $scope.top_Color;
	$scope.addColor = function(colorId) {
		if (findObjInObjsByID(colorId, $scope.selectedColor, "colorId")) {
			$scope.selectedColor.push(findObjByID(colorId, allColor, "colorId"));
		}
	};

	$scope.deleteColor = function(colorId) {
		for (var i = 0; i < $scope.selectedColor.length; i++) {
			if ($scope.selectedColor[i].colorId === colorId) {
				$scope.selectedColor.splice(i, 1);
				break;
			}
		}
	};

	var allSize = $scope.top_Size;
	$scope.addSize = function(sizeId) {
		if (findObjInObjsByID(sizeId, $scope.selectedSize, "sizeId")) {
			$scope.selectedSize.push(findObjByID(sizeId, allSize, "sizeId"));
		}
	};

	$scope.deleteSize = function(sizeId) {
		for (var i = 0; i < $scope.selectedSize.length; i++) {
			if ($scope.selectedSize[i].sizeId === sizeId) {
				$scope.selectedSize.splice(i, 1);
				break;
			}
		}
	};

	var allClass = $scope.top_Class;
	$scope.addClass = function(classId) {
		if (findObjInObjsByID(classId, $scope.selectedClass, "classId")) {
			$scope.selectedClass.push(findObjByID(classId, allClass, "classId"));
		}
	};

	$scope.deleteClass = function(classId) {
		for (var i = 0; i < $scope.selectedClass.length; i++) {
			if ($scope.selectedClass[i].classId === classId) {
				$scope.selectedClass.splice(i, 1);
				break;
			}
		}
	};

	var allStyle = $scope.top_Style;
	$scope.addStyle = function(styleId) {
		if (findObjInObjsByID(styleId, $scope.selectedStyle, "styleId")) {
			$scope.selectedStyle.push(findObjByID(styleId, allStyle, "styleId"));
		}
	};

	$scope.deleteStyle = function(styleId) {
		for (var i = 0; i < $scope.selectedStyle.length; i++) {
			if ($scope.selectedStyle[i].styleId === styleId) {
				$scope.selectedStyle.splice(i, 1);
				break;
			}
		}
	};

	//通过ID查找并返回Obj
	function findObjByID(ID, allObjs, xxxID) {
		for (var i = 0; i < allObjs.length; i++) {
			if (ID === allObjs[i][xxxID]) {
				return allObjs[i];
			}
		}
	}

	//通过ID查找allObjs是否已存在Obj，已存在返回false
	function findObjInObjsByID(ID, allObjs, xxxID) {
		for (var i = 0; i < allObjs.length; i++) {
			if (ID === allObjs[i][xxxID]) {
				return false;
			}
		}
		return true;
	}

	$scope.searchGoods = function() {
		if ($scope.id === "" || $scope.id === null) {
			alert("请填写服装ID！");
			return;
		}

		$scope.name = "";
		$scope.price = "";
		$scope.brandId = "";
		$scope.description = "";
		$scope.remark = "";

		$scope.colorId = "";
		$scope.selectedColor = [];
		$scope.sizeId = "";
		$scope.selectedSize = [];
		$scope.classId = "";
		$scope.selectedClass = [];
		$scope.styleId = "";
		$scope.selectedStyle = [];

		var goodsData = {
			"goods.goodsId": $scope.id
		};

		var postCfg = {
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			transformRequest: function(data) {
				return $.param(data);
			}
		};

		$http.post('searchGoodsByID', goodsData, postCfg).then(function successCallback(response) {
			if (response.data[0].Nothing === "Nothing") {
				alert("查询不到！");
			} else {
				fillInGoodsInfo(response.data[0]);
			}

			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("查询操作失败!");
			console.log("fail：" + response);
		});

		function fillInGoodsInfo(goods) {
			$scope.name = goods.name;
			$scope.price = goods.price;
			$scope.brandId = goods.brandId;
			$scope.description = goods.description;
			$scope.remark = goods.remark;

			var colorIds = goods.colorId.split(",");
			for (var i = 0; i < colorIds.length; i++) {
				$scope.addColor(parseInt(colorIds[i]));
			}
			var sizeIds = goods.sizeId.split(",");
			for (var i = 0; i < sizeIds.length; i++) {
				$scope.addSize(parseInt(sizeIds[i]));
			}
			var classIds = goods.classId.split(",");
			for (var i = 0; i < classIds.length; i++) {
				$scope.addClass(parseInt(classIds[i]));
			}
			var styleIds = goods.styleId.split(",");
			if (styleIds[0] !== "") {
				for (var i = 0; i < styleIds.length; i++) {
					$scope.addStyle(parseInt(styleIds[i]));
				}
			}
		}
	};

}]);